using System;
using System.Linq;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace OpenLightGroup.Common
{
    public class ActionTokenCollection<T> 
        : ObservableCollection<ActionToken<T>>
    {
        private bool _hasActions;

        public ActionTokenCollection()
        {
            this.CollectionChanged += new NotifyCollectionChangedEventHandler(ActionTokenCollection_CollectionChanged);
        }

        void ActionTokenCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            this.HasActions = this.Items.Count > 0;
        }

        public bool HasActions
        {
            get { return _hasActions; }
            set
            {
                if (_hasActions != value)
                {
                    _hasActions = value;
                    base.OnPropertyChanged(new PropertyChangedEventArgs("HasActions"));
                }
            }
        }

        public Guid CreateToken(T value)
        {

            ActionToken<T> token = new ActionToken<T>(Guid.NewGuid(), value);

            this.Add(token);

            return token.Id;

        }

        public void Add(Guid id, T value)
        {

            this.Add(new ActionToken<T>(id, value));
        }

        public void Remove(Guid id)
        {
            var item = (from i in this.Items where i.Id == id select i).FirstOrDefault();

            if (item != null)
            {
                base.Remove(item);
            }
        }

    }

 
    public class ActionTokenCollection
        : ActionTokenCollection<string>
    {

    }


}
